package com.example.q_kang.hhh.setting;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import com.example.q_kang.hhh.R;
import com.sskj.base.MVPActivity;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.adapter.slimadapter.SlimAdapter;
import com.sskj.base.adapter.slimadapter.SlimInjector;
import com.sskj.base.base.App;
import com.sskj.base.bean.MyBanksBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.HideUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.functions.Consumer;

/**
 * @author Hey
 * Create at  2019/04/10
 */
public class BindingBankActivity extends MVPActivity<BindingBankPresenter> {


    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_add_bank)
    Button btnAddBank;
    private SlimAdapter<MyBanksBean.BankBean> slimAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_binding_bank;
    }

    @Override
    public BindingBankPresenter getPresenter() {
        return new BindingBankPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(view -> finish());
        ClickUtil.click(300, btnAddBank, view -> SetBankActivity.start(this, null));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapter = SlimAdapter.create().register(R.layout.item_bank, new SlimInjector<MyBanksBean.BankBean>() {
            @Override
            public void onInject(MyBanksBean.BankBean data, IViewInjector injector) {
                injector.image(R.id.iv_bank, data.getImg())
                        .text(R.id.tv_bank, data.getName_cn())
                        .text(R.id.tv_bank_card, data.getBank_account())
                        .text(R.id.tv_default, data.getBank_default().equals("1") ? App.INSTANCE.getString(R.string.appJavaBindingBankActivity2) : App.INSTANCE.getString(R.string.appJavaBindingBankActivity3))
                        .clicked(R.id.tv_default, view -> {
                            showLoading();
                            mPresenter.setBankDefault(data.getId());
                        })
                        .clicked(R.id.tv_edit, view -> {
                            SetBankActivity.start(BindingBankActivity.this, data);
                        })
                        .clicked(R.id.rl_delete, view -> {
                            showLoading();
                            mPresenter.deleteBank(data.getId());
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
//        Flowable.rangeLong(0, 5)
//                .map(i -> i + "")
//                .toList()
//                .subscribe(strings -> slimAdapter.updateData(strings));
    }

    @Override
    public void initData() {
        mPresenter.getBankList();
        showLoading();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mPresenter.getBankList();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, BindingBankActivity.class);
        context.startActivity(intent);
    }

    public void showUserBanks(List<MyBanksBean.BankBean> userBanks) {
        slimAdapter.updateData(userBanks);

    }
}
